package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.services.GoodsService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @Date 2018/12/16  15:47
 * @Author JIE
 */
@RestController
public class GoodsController {


    @Autowired
    private GoodsService goodsService;


    /**
     * 分页查询spu
     * @param page
     * @param rows
     * @param saleable
     * @param key
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<Spu>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "key",required = false) String key
    ){
        PageResult<Spu> spuPageResult = goodsService.querySpuByPage(page, rows, saleable, key);
        return ResponseEntity.ok(spuPageResult);
    }

    /**
     * 商品的新增
     * @param spu
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody Spu spu){
        goodsService.saveGoods(spu);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spu的id查询SpuDetail的信息
     *
     */
     @GetMapping("/spu/detail/{id}")
     public ResponseEntity<SpuDetail> queryDetailById(@PathVariable("id")Long id){
         SpuDetail spuDetail = goodsService.queryDetailById(id);
         return ResponseEntity.ok(spuDetail);
     }

    /**
     * 根据spu查询下面的所有sku信息
     * @param spuId
     * @return
     */
     @GetMapping("sku/list")
     public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id")Long spuId){
         List<Sku> skuList = goodsService.querySkuBySpuId(spuId);
         return ResponseEntity.ok(skuList);
     }

    /**
     * 修改商品数据
     * @param spu
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody Spu spu){
        goodsService.updateGoods(spu);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除商品
     * @param spuId
     * @return
     */
    @DeleteMapping("goods/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id")Long spuId){
        goodsService.deleteGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 下架商品
     */
    @GetMapping("unShelveGoods/{id}")
    public ResponseEntity<Void> unShelveGoods(@PathVariable("id")Long spuId){
        goodsService.unShelveGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 上架商品
     * @param spuId
     * @return
     */
    @GetMapping("putawayGoods/{id}")
    public ResponseEntity<Void> putawayGoods(@PathVariable("id")Long spuId){
        goodsService.putawayGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *  根据spu的id查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        Spu spu = goodsService.querySpuById(id);
        return ResponseEntity.ok(spu);
    }

}

